package pachong;

import cn.hutool.core.util.NumberUtil;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Date 2024/6/7 9:20
 * @Author Tsing
 * 1.导入组件
* <dependency>
*     <groupId>org.jsoup</groupId>
*     <artifactId>jsoup</artifactId>
*     <version>1.17.2</version>
* </dependency>
 */
public class PaChongDemo {


    public static void main(String[] args) {
//        demo1();

//        mehtod();

        method2();

//        method3();

//        method4();



    }

    private static void demo1() {
        String url = "https://index.iresearch.com.cn/new/#/app/list";

        try {
//            Document doc = Jsoup.connect(url).get();

            Document doc= Jsoup.connect(url).timeout(50000000).maxBodySize(0).get();
            Elements elements = doc.select("h3.ui-typography--headline a");

            for (Element element : elements) {
                String title = element.text();
                String link = element.attr("href");
                System.out.println("Title: " + title);
                System.out.println("Link: " + link);
                System.out.println("----------------------------------");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void mehtod(){

        try {
            //Htmlunit模拟的浏览器，设置css,js等支持及其它的一些简单设置
            WebClient browser = new WebClient();
            browser.getOptions().setCssEnabled(false);
            browser.getOptions().setJavaScriptEnabled(true);
            browser.getOptions().setThrowExceptionOnScriptError(false);

            //获取页面
            HtmlPage htmlPage = browser.getPage("https://index.iresearch.com.cn/new/#/app/list");
            //设置等待js的加载时间
            browser.waitForBackgroundJavaScript(3000);
            //使用xml的方式解析获取到jsoup的document对象
            Document doc = Jsoup.parse(htmlPage.asXml());

            Element elements = doc.getElementById("indexList");
            System.out.println(elements);
            System.out.println(doc);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void method2(){
        // 设置 ChromeDriver 路径
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\23083\\Desktop\\chromedriver-win64\\chromedriver.exe");
//        System.setProperty("webdriver.edge.driver", "C:\\Python310\\msedgedriver.exe");

        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless");  // 无头模式
        options.addArguments("--disable-dev-shm-usage");
        options.addArguments("--no-sandbox");
        options.addArguments("--remote-debugging-port=30987");
        WebDriver driver = new ChromeDriver(options);
//        driver.get("https://index.iresearch.com.cn/new/#/app/list");
        driver.get("https://www.selenium.dev/");

        // 等待页面加载完成
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        String html = driver.getPageSource();
//        driver.quit();

        Document doc = Jsoup.parse(html);
        System.out.println(doc);

    }

    public static void method3() {
        try (final WebClient webClient = new WebClient()) {
            // 启用JavaScript支持
            webClient.getOptions().setJavaScriptEnabled(true);

            // 禁用Css渲染
            webClient.getOptions().setCssEnabled(true);

            // 获取网页
            HtmlPage page = webClient.getPage("https://index.iresearch.com.cn/new/#/app/list");

            // 打印网页内容
            System.out.println(page.asXml());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void method4(){
        WebDriver driver = new ChromeDriver();

        driver.get("https://selenium.dev");

        driver.quit();

    }

}




